unit Service.Report;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf,
  FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.Stan.Async,
  FireDAC.Phys, FireDAC.VCLUI.Wait, Data.DB, FireDAC.Comp.Client, frxClass,
  frxDBSet, frxExportPDF, mvc.DB, mvc.DataSet,IOUtils;

type
  TReport = record
  public

    function expPDF(guid: string): string;
  end;

implementation

{ TReport }

function TReport.expPDF(guid: string): string;
var
  frxReport1: TfrxReport;
  frxPDFExport1: TfrxPDFExport;
  frxdbset: TfrxDBDataset;
  db: IConn;
  ds: IDataSet;
  path:string;
begin
  db := IIConn;
  path:='ReportTmp/';
  ds := db.DB.Find('select * from users');

  frxReport1 := TfrxReport.Create(nil);
  frxPDFExport1 := TfrxPDFExport.Create(nil);
  frxdbset := TfrxDBDataset.Create(nil);

  try
    frxdbset.UserName:='frxDBDataset1';
    frxdbset.DataSet := ds.DS;
    frxReport1.DataSets.Add(frxdbset);
    frxPDFExport1.ShowDialog := false;
    frxPDFExport1.ShowProgress := false;

    if (frxReport1.LoadFromFile('report/report.fr3')) then
    begin
      frxReport1.PrepareReport();
      if not TDirectory.Exists(path) then
        TDirectory.CreateDirectory(path);
      frxPDFExport1.FileName := path + guid + '.pdf';
      frxReport1.Export(frxPDFExport1);
    end;
    Result := frxPDFExport1.FileName;
  finally

    frxReport1.Free;
    frxdbset.Free;
    frxPDFExport1.Free;
  end;
end;

end.

